package com.scut.online_black_market.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.scut.online_black_market.dto.ErrandResponse;
import com.scut.online_black_market.dto.PostErrandInfo;
import com.scut.online_black_market.dto.SuccessResponse;
import com.scut.online_black_market.exception.PostErrandException;
import com.scut.online_black_market.exception.UniversalException;
import com.scut.online_black_market.service.ErrandService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigInteger;
import java.util.Optional;

@Api(value = "跑腿任务交易接口",tags = "跑腿任务交易接口")
@RestController
@RequestMapping(value = "/api/errandTrade")
public class ErrandController {

    @Resource
    private ErrandService errandService;

    @ApiOperation("代跑腿发布")
    @RequestMapping(value="/postErrand",method = RequestMethod.POST)
    public ResponseEntity<SuccessResponse>postErrand(@RequestBody PostErrandInfo postErrandInfo){
        try{
            errandService.postErrand(postErrandInfo);
            return ResponseEntity.ok(new SuccessResponse(true,"发布代跑腿成功"));
        }catch (PostErrandException e){
            return ResponseEntity.ok(new SuccessResponse(false,e.getMessage()));
        }
    }

    @ApiOperation("根据订单id查询代跑腿信息")
    @RequestMapping(value = "/queryByErrandId/{errandId}",method = RequestMethod.GET)
    public ResponseEntity<ErrandResponse> queryByErrandId(@PathVariable BigInteger errandId){

        try {
            ErrandResponse errandResponse = errandService.queryByErrandId(errandId);
            return ResponseEntity.ok(errandResponse);
        } catch (UniversalException e) {
            return ResponseEntity.of(Optional.empty());
        }
    }

    @ApiOperation("分页查询跑腿任务")
    @RequestMapping(value="/queryErrandTaskByPage/{pageNo}/{pageSize}/{typeId}",method = RequestMethod.GET)
    public ResponseEntity<IPage<ErrandResponse>> queryShoppingGuideByPage (
            @ApiParam("页号数")@PathVariable short pageNo,
            @ApiParam("页大小")@PathVariable short pageSize,
            @ApiParam("查询种类")@PathVariable short typeId) {

        IPage<ErrandResponse> result= errandService.queryErrandByPage(pageNo,pageSize,typeId);
        return ResponseEntity.ok(result);
    }
    @ApiOperation("接受跑腿")
    @RequestMapping(value = "/acceptErrand/{errandID}/{acceptorID}",method = RequestMethod.GET)
    public ResponseEntity<SuccessResponse> acceptErrand(@ApiParam(value = "订单号") @PathVariable BigInteger errandID,
                                                        @ApiParam(value = "接受者ID") @PathVariable String acceptorID){
        try {
            errandService.accept(errandID,acceptorID);
            return ResponseEntity.ok(new SuccessResponse(true,"成功接受跑腿任务"));
        } catch (UniversalException e) {
            return ResponseEntity.ok(new SuccessResponse(false,e.getMessage()));
        }
    }
}
